//
//  GoodsModel.swift
//  RPay
//
//  Created by CharlieLiao on 2017/7/23.
//  Copyright © 2017年 Charlie. All rights reserved.
//

import UIKit

class GoodsModel: NSObject, NSCoding, NSCopying {
    
    let id: String
    let name: String
    var num: Int = 1
    let priceStr: String
    let price: Double
    
    required init(id: String, name: String, price: String) {
        self.id = id
        self.name = name
        self.priceStr = price
        self.price = Double(priceStr)!
        super.init()
    }
    
    required init(coder aDecoder: NSCoder) {
        id = aDecoder.decodeObject(forKey: "id") as! String
        name = aDecoder.decodeObject(forKey: "name") as! String
        num = aDecoder.decodeInteger(forKey: "num")
        priceStr = aDecoder.decodeObject(forKey: "price") as! String
        price = Double(priceStr)!
        super.init()
    }
    
    func encode(with aCoder: NSCoder) {
        aCoder.encode(id, forKey: "id")
        aCoder.encode(name, forKey: "name")
        aCoder.encode(num, forKey: "num")
        aCoder.encode(priceStr, forKey: "price")
    }
    
    func copy(with zone: NSZone? = nil) -> Any {
        let new = type(of: self).init(id: id, name: name, price: priceStr)
        new.num = num
        return new
    }
}
